﻿#include "ssRandom.h"


namespace StarSeeker
{

ssRandom::ssRandom(void)
{
	m_seed = 1l;
	m_multiplier = 0x015a4e35l;
	m_increment = 1l;
}

void ssRandom::Seed(u32 sd)
{
	m_seed = sd;
}

i32 ssRandom::Rand(void)
{
	return (i32)RandU32();
}

u32 ssRandom::RandU32(void)
{
	m_seed = m_multiplier * m_seed + m_increment;
	return((m_seed >> 16) & 0x7fff);
}

i32 ssRandom::Rand(i32 min, i32 max)
{
	if (max>min)
	{
		return Rand()%(max+1-min) + min;
	}
	else
	{
		return min;
	}
}

u32 ssRandom::RandU32(u32 min, u32 max)
{
	if (max>min)
	{
		return RandU32()%(max+1-min) + min;
	}
	else
	{
		return min;
	}
}

i32 ssRandom::RandEven(i32 min, i32 max)
{
	i32 num = Rand(min, max);
	if (num%2)
	{
		if (num==min)
		{
			++num;
		}
		else if (num==max)
		{
			--num;
		}
		else
		{
			num = (Rand()%2==1)?(num+1):(num-1);
		}
	}
	return num;
}

bool ssRandom::RandBool(void)
{
	return RandU32()%2==0?false:true;
}

bool ssRandom::RandPercent(i32 numerator, i32 denominator)
{
	return Rand(1, denominator)<=numerator;
}

}
